package com.douma.config;

import org.redisson.Redisson;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

/**
 * 抖码课堂，专注中国程序员基础内功修炼
 * 官网：douma-tech.com
 * 公众号：抖码课堂
 *
 * @作者 : 老汤
 */
@Configuration
public class RedissonConfig {

    @Value("${spring.redis.host}")
    private String ip;

    @Value("${spring.redis.port}")
    private String port;

    @Value("${spring.redis.password}")
    private String password;

    @Value("${spring.redis.database}")
    private int db;

    @Bean
    public RedissonClient redisson() throws IOException {
        Config config = new Config();
        SingleServerConfig singleServerConfig = config.useSingleServer();
        singleServerConfig.setAddress("redis://" + ip + ":" + port);
        singleServerConfig.setPassword(password);
        singleServerConfig.setDatabase(db);
        RedissonClient redissonClient = Redisson.create(config);
        return redissonClient;
    }

    @Bean
    public RBloomFilter<Long> redisBloomFilter(RedissonClient redissonClient) throws IOException {
        RBloomFilter<Long> bloomFilter =
                redissonClient.getBloomFilter("shopids");
        bloomFilter.tryInit(1000000, 0.02);
        return bloomFilter;
    }
}
